package de.fhaugsburg.games.boardgames.scrabble.consoleui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import de.fhaugsburg.games.boardgames.scrabble.IScrabbleEngine;

public class CommandProcessor_old implements IGuiMessage {

	/**
	 * available commands for user input
	 */
	public enum Command {
		NEWGAME, ADD, HELP, EXIT,
		NOVALUE;
		public static Command toCommand(String s){
			 try {
		        return valueOf(s);
	        } catch (Exception ex) {
	            return NOVALUE;
	        }
		}
	}

	
	
	
	
	private BoardView boardView;
	//private IScrabbleEngine engine;
	

	
	
	/**
	 * Constructor
	 * @param engine
	 * @param myboard
	 */
	public CommandProcessor_old(IScrabbleEngine engine, BoardView myboard){
		//this.engine = engine;
		this.boardView = myboard;
	}
	
	
	
	public void process(){
		getUserInput();
	}
	
	
	
	public void getUserInput() {
		String command = "";
		String[] commandTokens;
		int commandTokensCount;
		//boolean valid = false;
		while(true){
			
		
			//this is our command prompt
//			if (engine.getGameState() == GameState.WAITING_FOR_INPUT){
//				System.out.print(engine.getActivePlayer().getPlayerName() + " ");
//			}
//			//System.out.println(engine.getGameState().toString());			//DEBUG
			System.out.print("#> ");
			
			
			BufferedReader in = new BufferedReader(new InputStreamReader( System.in ) );
			try {
				command = in.readLine();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if (command.length() > 0){
				commandTokens = command.split(" ");
				commandTokensCount = commandTokens.length; 
				

				switch(Command.toCommand(commandTokens[0].toUpperCase())){
				case NEWGAME:
						if (commandTokensCount > 1){
							//start singleplayer game (1 CPU player)
							if (commandTokens[1].toUpperCase().equals("SP")){
								System.out.println("Starting new singleplayer game.");
								//valid = true;
//								try {
////									engine.newGame(2);
//								} catch (GameException e) {
//									// TODO Auto-generated catch block
//									e.printStackTrace();
//								}
//								try {
//									engine.startGame();
//								} catch (GameException e) {
//									// TODO Auto-generated catch block
//									e.printStackTrace();
//								}
							//start multiplayer game
							}else if (commandTokens[1].toUpperCase().equals("MP")){
								System.out.println("Starting new multiplayer game.");
								//valid = true;
//								try {
//									engine.newGame(2);
//								} catch (GameException e) {
//									// TODO Auto-generated catch block
//									e.printStackTrace();
//								}
//								try {
//									engine.startGame();
//								} catch (GameException e) {
//									// TODO Auto-generated catch block
//									e.printStackTrace();
//								}
							}else{
								System.err.println("Please select playermode: SP | MP.");
								printHelp();
							}
						}else{
							System.err.println("Please start new game with a playermode: SP or MP.");
							printHelp();
						}
					break;
				case ADD:
					if (commandTokensCount > 1){
						// is there a game running?
//						if (engine.getGameState() != GameState.GAMEOVER && engine.getGameState() != GameState.NOGAME){
//							try {
//								LetterPiece letter = LetterPiece.valueOf(commandTokens[1]);
//								int row = Integer.parseInt(commandTokens[2]) - 1;
//								int column = Integer.parseInt(commandTokens[3]) - 1;
//								engine.setLetter(letter, row, column);
//							}catch(Exception e){
//								System.err.println("invalid command");
//								printHelp();
//							}
//							repaint();
//							/*try{
//								engine.makeMove(newMove);
//								//valid = true;
//								
//							}catch(IllegalMoveException e){
//								System.err.println("Invalid move: " + e.toString());
//							}*/
//						}else{
//							System.err.println("Before entering moves, please start a new game.");
//							printHelp();
//						}
					}else{
						System.err.println("Please enter ADD <columnNumber>.");
						printHelp();
					}
					break;
				case HELP:
						printHelp();
					break;
				case EXIT:
						System.exit(0);
					break;
				default:
					System.err.println("Unknown command.");
					printHelp();
					break;
				}
			}else{ //if command
				System.err.println("Unknown command.");
				printHelp();
			}
		} // while
	}

	
	
	/**
	 * prints available commands
	 */
	public void printHelp(){
		System.out.println(
			 "Commands:\n"+
			 "NEWGAME <SP | MP>   * in Single Player or Multi Player mode\n"+
			 "ADD columnnr        * an integer in range\n"+
			 "HELP\n"+
			 "EXIT");
	}
		
	
	/**
	 * repaints the current state of the game
	 */
	public void repaint() {
		boardView.render();
//		if (engine.getGameState() == GameState.GAMEOVER){
////			System.out.println("WINNER: " +
////					engine.getWinningPlayer().getPlayerName() +
////					" (" + engine.getWinningPlayer().getSymbol().toString() + ") "
////				);
//		}
	}



	public void idle() {
		//getUserInput();		// results in endless recursion!
	}
}